using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Timers;
using YukiFX5017.Core;

namespace YukiFX5017
{
    public class DroppedItems
    {
        public static bool PlaceFree(ushort x, ushort y, ushort map)
        {
            bool ret = true;
            if (DMaps.Loaded)
                if (((DMap)DMaps.H_DMaps[map]).GetCell(x, y).NoAccess)
                    ret = false;

            foreach (var DE in Kernel.AllDroppedItems)
            {
                DroppedItem ITem = DE.Value;
                if (ITem.X == x)
                    if (ITem.Y == y)
                        if (ITem.Map == map)
                            ret = false;
            }
            return ret;
        }
        public static DroppedItem DropItem(string item, uint x, uint y, uint map, uint money, Character name)
        {
            uint uid = (uint)General.Rand.Next(1, 10000000);
            while (Kernel.AllDroppedItems.ContainsKey(uid))
                uid = (uint)General.Rand.Next(1, 10000000);


            uint NewX = x;
            uint NewY = y;


            if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
            {
                NewX += 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
            {
                NewX -= 1;
                NewY += 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
            {
                NewX -= 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
            {
                NewX -= 1;
                NewY -= 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
            {
                NewY -= 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
            {
                NewY += 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
            {
                NewX += 1;
                NewY -= 1;
            }
            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
            {
                NewY += 1;
                NewX += 1;
            }
            #region Relocate Pos
            if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
            {
            }
            else
            {
                if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                {
                    NewX += 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                {
                    NewX -= 1;
                    NewY += 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                {
                    NewX -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                {
                    NewX -= 1;
                    NewY -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                {
                    NewY -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                {
                    NewY += 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                {
                    NewX += 1;
                    NewY -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                {
                    NewY += 1;
                    NewX += 1;
                }
                if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                {
                }
                else
                {
                    if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                    {
                        NewX += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewX -= 1;
                        NewY += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                    {
                        NewX -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewX -= 1;
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewY += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewX += 1;
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewY += 1;
                        NewX += 1;
                    }
                }
            }
            if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
            {
            }
            else
            {
                if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                {
                    NewX += 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                {
                    NewX -= 1;
                    NewY += 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                {
                    NewX -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                {
                    NewX -= 1;
                    NewY -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                {
                    NewY -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                {
                    NewY += 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                {
                    NewX += 1;
                    NewY -= 1;
                }
                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                {
                    NewY += 1;
                    NewX += 1;
                }
                if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                {
                }
                else
                {
                    if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                    {
                        NewX += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewX -= 1;
                        NewY += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                    {
                        NewX -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewX -= 1;
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewY += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewX += 1;
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewY += 1;
                        NewX += 1;
                    }
                }
                if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                {
                }
                else
                {
                    if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                    {
                        NewX += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewX -= 1;
                        NewY += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                    {
                        NewX -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewX -= 1;
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewY += 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                    {
                        NewX += 1;
                        NewY -= 1;
                    }
                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                    {
                        NewY += 1;
                        NewX += 1;
                    }
                    if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                    {
                    }
                    else
                    {
                        if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                        {
                            NewX += 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                        {
                            NewX -= 1;
                            NewY += 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                        {
                            NewX -= 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                        {
                            NewX -= 1;
                            NewY -= 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                        {
                            NewY -= 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                        {
                            NewY += 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                        {
                            NewX += 1;
                            NewY -= 1;
                        }
                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                        {
                            NewY += 1;
                            NewX += 1;
                        }
                        if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                        {
                        }
                        else
                        {
                            if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                            {
                                NewX += 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                            {
                                NewX -= 1;
                                NewY += 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                            {
                                NewX -= 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                            {
                                NewX -= 1;
                                NewY -= 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                            {
                                NewY -= 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                            {
                                NewY += 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                            {
                                NewX += 1;
                                NewY -= 1;
                            }
                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                            {
                                NewY += 1;
                                NewX += 1;
                            }
                            if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                            {
                            }
                            else
                            {
                                if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                                {
                                    NewX += 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                                {
                                    NewX -= 1;
                                    NewY += 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                                {
                                    NewX -= 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                                {
                                    NewX -= 1;
                                    NewY -= 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                                {
                                    NewY -= 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                                {
                                    NewY += 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                                {
                                    NewX += 1;
                                    NewY -= 1;
                                }
                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                                {
                                    NewY += 1;
                                    NewX += 1;
                                }
                                if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                                {
                                }
                                else
                                {
                                    if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                                    {
                                        NewX += 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                                    {
                                        NewX -= 1;
                                        NewY += 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                                    {
                                        NewX -= 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                                    {
                                        NewX -= 1;
                                        NewY -= 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                                    {
                                        NewY -= 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                                    {
                                        NewY += 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                                    {
                                        NewX += 1;
                                        NewY -= 1;
                                    }
                                    else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                                    {
                                        NewY += 1;
                                        NewX += 1;
                                    }
                                    if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                                    {
                                    }
                                    else
                                    {
                                        if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                                        {
                                            NewX += 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                                        {
                                            NewX -= 1;
                                            NewY += 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                                        {
                                            NewX -= 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                                        {
                                            NewX -= 1;
                                            NewY -= 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                                        {
                                            NewY -= 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                                        {
                                            NewY += 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                                        {
                                            NewX += 1;
                                            NewY -= 1;
                                        }
                                        else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                                        {
                                            NewY += 1;
                                            NewX += 1;
                                        }
                                        if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                                        {
                                        }
                                        else
                                        {
                                            if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                                            {
                                                NewX += 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                                            {
                                                NewX -= 1;
                                                NewY += 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                                            {
                                                NewX -= 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                                            {
                                                NewX -= 1;
                                                NewY -= 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                                            {
                                                NewY -= 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                                            {
                                                NewY += 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                                            {
                                                NewX += 1;
                                                NewY -= 1;
                                            }
                                            else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                                            {
                                                NewY += 1;
                                                NewX += 1;
                                            }
                                            if (PlaceFree((ushort)NewX, (ushort)NewY, (ushort)map))
                                            {
                                            }
                                            else
                                            {
                                                if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 0), (ushort)map))
                                                {
                                                    NewX += 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 1), (ushort)map))
                                                {
                                                    NewX -= 1;
                                                    NewY += 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y + 0), (ushort)map))
                                                {
                                                    NewX -= 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x - 1), Convert.ToUInt16(y - 1), (ushort)map))
                                                {
                                                    NewX -= 1;
                                                    NewY -= 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y - 1), (ushort)map))
                                                {
                                                    NewY -= 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 0), Convert.ToUInt16(y + 1), (ushort)map))
                                                {
                                                    NewY += 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y - 1), (ushort)map))
                                                {
                                                    NewX += 1;
                                                    NewY -= 1;
                                                }
                                                else if (DroppedItems.PlaceFree(Convert.ToUInt16(x + 1), Convert.ToUInt16(y + 1), (ushort)map))
                                                {
                                                    NewY += 1;
                                                    NewX += 1;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            DroppedItem ITEM = new DroppedItem(uid, item, NewX, NewY, map, money, name.Name);
            Kernel.AllDroppedItems.Add(uid, ITEM);

            return ITEM;
        }
    }

    public class DroppedItem
    {
        public uint UID;
        public string Item;
        public uint ItemId;
        public uint X;
        public uint Y;
        public uint Map;
        public uint Money;
        public string Owner;
        public int DestroyTimer = 0;

        public DroppedItem(uint uid, string item, uint x, uint y, uint map, uint money, string name)
        {
            UID = uid;
            Item = item;
            X = x;
            Y = y;
            Map = map;
            Money = money;
            Owner = name;

            string[] Splitter = item.Split('-');
            ItemId = uint.Parse(Splitter[0]);
        }

        public void Destroy()
        {
            Kernel.AllDroppedItems.Remove(UID);
            World.ItemDissappears(this);
        }
    }
}
